% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

%% Table 4 (columns 2-7) in the paper
% see extra files for columns 1 and 8
% set model in lines 28-29
% choose specification in line 47
% Note that case 7 "political risk as control" requires the purchase of the
% proprietary ICRG political risk data

%% Setup

clear; close all; clc
addpath('../Auxiliary_Files')

unconditional_model_dummy = 1 ; % 1 for upper panel, 0 for middle and lower panel
sign_dummy = 0; % -1: cut, 0: symmetric

%% Load data arrays for regression

load('../data_array_for_regression_stacked_by_variable_CDS_new')

lag_number = 4;
max_irf_horizon = 8;
time_fixed_effects_dummy = 1;
country_fixed_effects_dummy = 1;

add_residual_dummy = 1; % adds residuals from previous step to regression to increase efficiency;


%the naming of the following needs to be consistent with the field names in the pos structure
impulse_name = 'g_growth_fe';


var_dummy = 2; % baseline

switch var_dummy
    case 1 % baseline
        regressor_var_names={}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make sure spread is consistent with indicator if requested!
    case 2 % spread in deviations from global factor
        regressor_var_names={}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter_from_PC_spread_group_specific'}; %make sure spread is consistent with indicator if requested!
        time_fixed_effects_dummy = 0;
    case 3 % lags of g growth fe as control
        regressor_var_names={'g_growth_fe'}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make sure spread is consistent with indicator if requested!
    case 4 % lags of g, y, and default premium as controls
        regressor_var_names={'g_growth_realization','y_growth_realization'}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make sure spread is consistent with indicator if requested!
    case 5 % global controls
        regressor_var_names={'commodity_price_index_growth', 'Euro_high_yield_index', 'ECB_shadow_rate', 'FFR_shadow_rate', 'VIX'}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make su're spread is consistent with indicator if requested!
        time_fixed_effects_dummy = 0;
    case 6 % trade balance and deficit ratio as controls
        regressor_var_names={'NX_share', 'deficit_to_GDP_including_interpolated'}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make su're spread is consistent with indicator if requested!
    case 7 % political risk as control. Note: ICRG political risk not included in data set. Needs to be purchased!
        regressor_var_names={'ICRG_political_risk_Total_risk'}; %do not add spreads here, they are added automatically depending on indicator_dummy
        dependent_var_names={'g_growth_realization','y_growth_realization','spread_end_quarter'}; %make su're spread is consistent with indicator if requested!
end

indicator_dummy = 1;
% 1 = Country specific CDF with spread at the end of quarter

switch indicator_dummy
    case 1 % 1 = Country specific CDF with spread at the end of quarter
        Indicator_position=pos.empirical_cdf_country_group_specific_end_quarter;
        [regressor_var_names]=check_dependent_and_regressors_for_consistency(dependent_var_names,regressor_var_names,'spread_end_quarter',0,0);
    otherwise
        error('Case is not defined')
end
if var_dummy == 1 || var_dummy == 2
    regressor_var_names = {};
elseif var_dummy == 3 || var_dummy == 5 || var_dummy == 6 || var_dummy == 7
    regressor_var_names = regressor_var_names(1:end-1);
end

%% Shock Variable

fe_shocks_temp = data_array_for_regression_stacked_by_variable(:,:,pos.(impulse_name));

fe_shocks_negative = zeros(size(fe_shocks_temp));
fe_shocks_negative(fe_shocks_temp<0 | isnan(fe_shocks_temp)) = fe_shocks_temp(fe_shocks_temp<0 | isnan(fe_shocks_temp));
fe_shocks_positive = zeros(size(fe_shocks_temp));
fe_shocks_positive(fe_shocks_temp>0 | isnan(fe_shocks_temp)) = fe_shocks_temp(fe_shocks_temp>0 | isnan(fe_shocks_temp));
if any(any(fe_shocks_negative>0)) || any(any(fe_shocks_positive<0 ))
    error('Sign is wrong')
end

if unconditional_model_dummy == 1
    if sign_dummy==-1
        data_array_for_regression_impulse_only=cat(3,fe_shocks_negative,fe_shocks_positive);
    elseif sign_dummy==1
        data_array_for_regression_impulse_only=cat(3,fe_shocks_positive,fe_shocks_negative);
    elseif sign_dummy==0
        data_array_for_regression_impulse_only=cat(3,fe_shocks_temp);
    else
        error('undefined case for sign_dummy')
    end
elseif unconditional_model_dummy == 0
    % neg_shock*F(z)
    data_array_for_regression_g_stress_neg=fe_shocks_negative.*...
    data_array_for_regression_stacked_by_variable(:,:,Indicator_position);
    % neg_shock*(1-F(z))
    data_array_for_regression_g_nostress_neg=fe_shocks_negative.*...
    (1-data_array_for_regression_stacked_by_variable(:,:,Indicator_position));

    % pos_shock*F(z)
    data_array_for_regression_g_stress_pos=fe_shocks_positive.*...
    data_array_for_regression_stacked_by_variable(:,:,Indicator_position);
    % pos_shock*(1-F(z))
    data_array_for_regression_g_nostress_pos=fe_shocks_positive.*...
    (1-data_array_for_regression_stacked_by_variable(:,:,Indicator_position));

    if sign_dummy==-1
        data_array_for_regression_impulse_only=cat(3,...
            data_array_for_regression_g_stress_neg,data_array_for_regression_g_nostress_neg,...
            data_array_for_regression_g_stress_pos,data_array_for_regression_g_nostress_pos...
            );
    elseif sign_dummy==1
        data_array_for_regression_impulse_only=cat(3,...
            data_array_for_regression_g_stress_pos,data_array_for_regression_g_nostress_pos,...
            data_array_for_regression_g_stress_neg,data_array_for_regression_g_nostress_neg...
            );
    elseif sign_dummy==0
        % shock*F(z)
        data_array_for_regression_g_stress=fe_shocks_temp.*...
            data_array_for_regression_stacked_by_variable(:,:,Indicator_position);
        % shock*(1-F(z))
        data_array_for_regression_g_nostress=fe_shocks_temp.*...
            (1-data_array_for_regression_stacked_by_variable(:,:,Indicator_position));
        
        data_array_for_regression_impulse_only=cat(3,...
            data_array_for_regression_g_stress,data_array_for_regression_g_nostress...
            );
    end
else
    error('case not defined')
end
data_array_for_regression=data_array_for_regression_impulse_only;

%% Lagged regressors

if var_dummy == 5
    % contemporaneous global controls
    for var_iter=1:length(regressor_var_names)
        if unconditional_model_dummy
            data_array_control = data_array_for_regression_stacked_by_variable(:,:,pos.(regressor_var_names{var_iter}));
            data_array_for_regression = cat(3,data_array_for_regression,data_array_control);
        else
            %F(z)*X_{t-k}
            data_array_state_1 = data_array_for_regression_stacked_by_variable(:,:,pos.(regressor_var_names{var_iter})).*...
                data_array_for_regression_stacked_by_variable(:,:,Indicator_position);
            %(1-F(z))*X_{t-k}
            data_array_state_2 = data_array_for_regression_stacked_by_variable(:,:,pos.(regressor_var_names{var_iter})).*...
                (1-data_array_for_regression_stacked_by_variable(:,:,Indicator_position));
            
            data_array_for_regression=cat(3,data_array_for_regression,data_array_state_1,data_array_state_2);
        end
    end
else
    for var_iter=1:length(regressor_var_names)
        if unconditional_model_dummy
            data_array_control=data_array_for_regression_stacked_by_variable(:,:,pos.([regressor_var_names{var_iter},'_lag_1']):pos.([regressor_var_names{var_iter},'_lag_',num2str(lag_number)]));
            data_array_for_regression=cat(3,data_array_for_regression,data_array_control);
        else
            %F(z)*X_{t-k}
            data_array_state_1 = data_array_for_regression_stacked_by_variable(:,:,pos.([regressor_var_names{var_iter},'_lag_1']):pos.([regressor_var_names{var_iter},'_lag_',num2str(lag_number)])).*...
                (repmat(data_array_for_regression_stacked_by_variable(:,:,Indicator_position),[1,1,lag_number]));
            %(1-F(z))*X_{t-k}
            data_array_state_2 = data_array_for_regression_stacked_by_variable(:,:,pos.([regressor_var_names{var_iter},'_lag_1']):pos.([regressor_var_names{var_iter},'_lag_',num2str(lag_number)])).*...
                (repmat(1-data_array_for_regression_stacked_by_variable(:,:,Indicator_position),[1,1,lag_number]));
            
            data_array_for_regression=cat(3,data_array_for_regression,data_array_state_1,data_array_state_2);
        end
    end
end
%% Run regression

if unconditional_model_dummy == 1
    theta_mat=NaN(1+max_irf_horizon,1,length(dependent_var_names));
    se_mat=NaN(1+max_irf_horizon,1,length(dependent_var_names));
else
    theta_mat=NaN(1+max_irf_horizon,2,length(dependent_var_names));
    se_mat = NaN(1+max_irf_horizon,2,length(dependent_var_names));
    test_stat_mat = NaN(1+max_irf_horizon,length(dependent_var_names));
    test_p_mat = NaN(1+max_irf_horizon,length(dependent_var_names));
end

for var_iter=1:length(dependent_var_names)
    for horizon_iter = 0:max_irf_horizon
        %construct matrices without NaN
        if horizon_iter==0
            dependent_variable=squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.(dependent_var_names{var_iter})));
            yhat_full=NaN(size(dependent_variable));
            [dependent_variable_for_run,data_array_for_regression_for_run,time_indices_non_NaN,country_indices_non_NaN]=create_regression_matrices_no_NaN(dependent_variable,data_array_for_regression,data_array_for_regression_stacked_by_variable,pos,country_indicator_names_mapping,time_fixed_effects_dummy,country_fixed_effects_dummy);
            % if var_iter==3 && horizon_iter==0
            %    generate_descriptives_table;
            % end
            if var_iter==3 && horizon_iter==0
               generate_nobs_ncountry;
            end
            %run regression
            [theta,stdDK,~,CovDK,yhat] = HszDk5cPs(dependent_variable_for_run,ones(size(dependent_variable_for_run,1),1),data_array_for_regression_for_run,1,7,1);
            
            if add_residual_dummy && ~strcmp(impulse_name,dependent_var_names(var_iter)) %when regression G on itself, residuals are 0
                yhat_full(time_indices_non_NaN,country_indices_non_NaN)=yhat;
                resids=dependent_variable-yhat_full;
            else
                resids=[];
            end
        else
            dependent_variable=squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.([dependent_var_names{var_iter},'_lead_',num2str(horizon_iter)])));
            yhat_full=NaN(size(dependent_variable));
            
            [dependent_variable_for_run,data_array_for_regression_for_run,time_indices_non_NaN,country_indices_non_NaN]=create_regression_matrices_no_NaN(dependent_variable,cat(3,data_array_for_regression,resids),data_array_for_regression_stacked_by_variable,pos,country_indicator_names_mapping,time_fixed_effects_dummy,country_fixed_effects_dummy);
            %run regression
            [theta,stdDK,~,CovDK,yhat] = HszDk5cPs(dependent_variable_for_run,ones(size(dependent_variable_for_run,1),1),data_array_for_regression_for_run,1,7,1);
            if add_residual_dummy
                yhat_full(time_indices_non_NaN,country_indices_non_NaN)=yhat;
                resids=dependent_variable-yhat_full;
            else
                resids=[];
            end
        end
        
        %save coefficients
        if unconditional_model_dummy
            theta_mat(1+horizon_iter,:,var_iter) = theta(1,1);
            se_mat(1+horizon_iter,:,var_iter)  = stdDK(1,1)';
        else
            theta_mat(1+horizon_iter,1:2,var_iter) = theta(1:2,1)';
            se_mat(1+horizon_iter,:,var_iter)  = stdDK(1:2,1)';
            Rmat = zeros(1,length(theta));
            Rmat(1,1:2) = [1,-1];
            Wstat = (Rmat*theta)'/(Rmat*CovDK*Rmat')*(Rmat*theta);
            test_stat_mat(1+horizon_iter, var_iter) = Wstat;
            test_p_mat(1+horizon_iter, var_iter) = 1 - chi2cdf(Wstat,1);
        end
    end
end
% disp('theta, std and t-stat');
% disp([theta,stdDKj,theta./stdDKj]);

%% Plot IRFs

if length(dependent_var_names) == 3
    subplot_index = [1,2,3];
    startsp = 1;
elseif length(dependent_var_names) == 4
    subplot_index = [2,4,1,5];
    startsp = 1;
else
    error('Specify Subplot Layout')
end


if sign_dummy == -1
    scaling = -0.25; % cut in government spending
    sign_plottitle = 'Gov. consumption cut';
elseif sign_dummy == 1
    scaling = 0.25; % cut in government spending
    sign_plottitle = 'Gov. consumption hike';
elseif sign_dummy == 0
    scaling = -0.25; % cut in government spending
    sign_plottitle = 'Gov. consumption cut (symmetric model)';
end

% only keep contemporaneous spread part

theta_mat_spread = squeeze(theta_mat(1,:,3));
se_mat_spread    = squeeze(se_mat(1,:,3));
p_mat_spread     = (1-normcdf(abs(theta_mat_spread)./se_mat_spread))*2;

% for syntax, see latexTable example in Data/Data/Auxiliary_Files 
input.transposeTable = 0;
input.dataNanString = '-';
input.tableColumnAlignment = 'l';
input.tableBorders = 0;
input.booktabs = 1;

if unconditional_model_dummy
    input.data = [scaling .* theta_mat_spread; abs(scaling) .* se_mat_spread; p_mat_spread; n_countries; n_obs];
    input.tableRowLabels = {'coeff.','se','pValue', 'n_country','n_obs'};
    input.tableColLabels = {'Linear'};
    input.dataFormat = {'%2.2f',1};
    input.tableCaption = 'Unconditional';
    input.tableLabel = 'uncond';
elseif unconditional_model_dummy == 0
    input.data = [scaling .* theta_mat_spread; abs(scaling) .* se_mat_spread; p_mat_spread; [abs(scaling) .* diff(theta_mat_spread) test_p_mat(1,3)]; [n_countries n_obs]];
    input.tableRowLabels = {'coeff.','se','pValue', 'diff_n_p','n_obs_country'};
    input.tableColLabels = {'Stress','NoStress'};
    input.dataFormat = {'%2.2f',2};
    input.tableCaption = 'Conditional';
    input.tableLabel = 'cond';
else
    error('not set up yet')
end

input.makeCompleteLatexDocument = 0;
latex = latexTable(input);
